﻿/*Sara Shaffer*/
/*Brandon Littell*/
/*GDD2- 2D Project*/
/*War for Stewtopia*/

package code {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.ui.Keyboard;
	import com.as3toolkit.ui.Keyboarder;
	import com.as3toolkit.events.KeyboarderEvent;
	import flash.geom.Point;
	
	public class Player extends GameObject {
		//TODO make rate adjustable
		private var rate:Number=10; //Rate at which player can be moved
		private var kb:Keyboarder; //Keyboarder
		private var gun:PlayerWeapon; //Player's weapon
		private var HP:Number; //Player's hit point count
		private var dead:Boolean; //Tells if player is dead
		//TODO Add second weapon
		
		// Returns the level to which this player belongs
		public function get CurrentLevel():Level { return parent as Level; }
		
		public function Player(spawnX:int, spawnY:int) {
			// constructor code
			super();
			this.scaleX = this.scaleY = .5;
			trace(this + "was created");
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			this.x = spawnX;
			this.y = spawnY;
			HP = 100;
			dead=false;
		}
		
		/*Initialization items for Player go in this method*/
		private function init(e:Event)
		{
			kb=new Keyboarder(this); //Create new keyboarder
			//Create weapon
			gun = new PlayerWeapon(0, 0, 250, BulletType.PEA);
			addChild(gun);
			HP=10;// TODO- set up loading system
			Keyboarder.Instance.addEventListener(KeyboarderEvent.FIRST_PRESS, onKP);
			Keyboarder.Instance.addEventListener(KeyboarderEvent.RELEASE, onKR);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/*Add the key listener when stage is finally ready*/
		private function onEnterFrame(event:Event):void
		{
			gun.update();
		}
		
		/*Key handlers*/
		/*Operations on key presses*/
		private function onKP(e:KeyboarderEvent): void{
			
			switch(e.keyCode)
			{
				case Keyboard.UP:
					//trace("up pressed");
					break;
				case Keyboard.DOWN:
					//trace("down pressed");
					break;
				case Keyboard.LEFT:
					//trace("left pressed");
					break;
				case Keyboard.RIGHT:
					//trace("right pressed");
					break;
				case Keyboard.SPACE:
					//trace("attack pressed");
					break;
				case Keyboard.F:
					//trace("gun 1 pressed");
					break;
				case Keyboard.G:
					//trace("gun 2 pressed");
					break;
				default:
					//trace ("some other key pressed "+ e.keyCode);
					break;
			}//end switch
		}//end on KP
		
		/*Operations on key release*/
		private function onKR(e:KeyboarderEvent): void
		{
			
			switch(e.keyCode)
			{
				case Keyboard.UP:
					//trace("up released");
					this.vy = 0;
					break;
				case Keyboard.DOWN:
					//trace("down released");
					this.vy = 0;
					break;
				case Keyboard.LEFT:
					//trace("left released");
					this.vx = 0;
					this.rotationY=0; //Realign ship
					break;
				case Keyboard.RIGHT:
					//trace("right released");
					this.vx = 0;
					this.rotationY=0; //Realign ship
					break;
				case Keyboard.SPACE:
					//trace("attack released");
					break;
				case Keyboard.F:
					//trace("gun 1 released");
					break;
				case Keyboard.G:
					//trace("gun 2 released");
					break;
				default:
					//trace ("some other key released "+ e.keyCode);
					break;
			}//end switch
		}//end onKR
		
		/*Key handlers on key downs*/
		public function keyDown(event:KeyboardEvent) 
		{ 
			  //Rotate this left if left key is hit; will not leave stage
			if(Keyboarder.keyIsDown(Keyboard.LEFT)) 
			{
				if (this.x>(this.width*0.5))
				{
					this.vx = -rate;
				}
				else
				{
					this.vx=0; //Stop movement left because boundary hit
				}
					//If y-rotation is below 45 degrees		
					if (this.rotationY<45)		
					{		
						this.rotationY+=5; //Rotate the ship five degrees on y axis		
					}
				
             } // end case Keyboard.LEFT
			
			//Rotate this right if right key is hit; will not leave stage
            if(Keyboarder.keyIsDown(Keyboard.RIGHT)) 
			{
				if (this.x<(stage.stageWidth-(this.width*0.5)))
				{
					this.vx = rate;
				}
				else
				{
					this.vx=0; //Stop movement right because boundary hit
				}
					//If y-rotation is above -45 degrees		
					if (this.rotationY>-45)		
					{		
						this.rotationY-=5; //Rotate the ship negative five degrees on y axis		
					}
				
            } // end case Keyboard.RIGHT
			
			//Speed up if UP is pressed but will not go past stage top
            if(Keyboarder.keyIsDown(Keyboard.UP))
			{
				if (this.y > (this.height*0.5))
				{
					this.vy = -rate;
				}
				else
				{
					this.vy=0; //Stop movement up because boundary hit
				}
            } // end case Keyboard.UP
			
			//Back up if Down is pressed but will not go past stage bottom
           if( Keyboarder.keyIsDown(Keyboard.DOWN) )
			{
				//Hard-coded value for HUD height
				if (this.y<(stage.stageHeight-this.height-57.55))
				{
					this.vy = rate;
					
				}
				else
				{
					this.vy=0; //Stop movement down because boundary hit
				}
            } // end case Keyboard.DOWN

			//Fires bullet
            if( Keyboarder.keyIsDown(Keyboard.SPACE) )
			{
				gun.fire();
             } // end case Keyboard.SPACE
			 
			 //Switches to pea shooter
			if (Keyboarder.keyIsDown(Keyboard.F))
			{
					removeChild(gun); //Remove old gun
					var ptPea:Point=this.globalToLocal(new Point(this.x, this.y)); //Convert global points to local
					gun= new PlayerWeapon(ptPea.x, ptPea.y, 250, BulletType.PEA); //Switch gun
					addChild(gun);//Add gun
			}//end case F
			//Switcher to RPP shooter
			if (Keyboarder.keyIsDown(Keyboard.G))
			{
					removeChild(gun); //remove old gun
					var ptRPP:Point=this.globalToLocal(new Point(this.x, this.y)); //Convert global points to local
					gun= new PlayerWeapon(ptRPP.x, ptRPP.y, 250, BulletType.RPP); //Switch gun
					addChild(gun); //Add gun
			}//end case G
        } // end keyDownHandler
		
		/*Plays the Player death animation*/
		private function dies():void
		{
			//Specify player death clip
			gotoAndPlay("death"); //Play death animation
			dead=true;
		}//end dies
		
		/*Cleanup*/
		public function cleanup()
		{
			removeChild(gun);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			Keyboarder.Instance.removeEventListener(KeyboarderEvent.FIRST_PRESS, onKP);
			Keyboarder.Instance.removeEventListener(KeyboarderEvent.RELEASE, onKR);
			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}//end playerCleanup
		
		/*Gives player damage and kills them below zero*/
		public function isHit(damage:Number)
		{
			if(HP <= damage)
			{
				trace("Dead");
				HP = 0;
				this.dies();//Kill the player
			}
			else
			{
				HP -= damage; //Take damage
			}
			//TODO specify damage animation
			//gotoAndPlay(""); //Play damage animation
			//If fatal hit

			//TODO Update HUD
		}//end isHit
		
		public function GetHP():int
		{
			return HP;
		}
		public function CheckForDeath():Boolean
		{
			if(currentLabel == "done")
			{
				return true;
			}
			
			return false;
		}
		
		/*Resets to normal after hit*/
		public function hitComplete():void
		{
			//TODO specify normal animation after hit animation is complete
			gotoAndPlay(""); //Play normal animation
		}//end hitComplete
		
		/*Add health from power up*/		
		public function powerUp(boost:Number)		
		{		
			//TODO- check against max HP		
			HP+=boost; //Add boost to HP		
		}//end powerUp
		
		/*Returns if player is dead or not*/
		public function isDead()
		{
			return dead;
		}
		
	}//end class
}//end package
